package com.ftf.tp.message.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ftf.tp.message.domain.directMessage.dos.NoticeSettingConfigDO;
import com.ftf.tp.message.domain.directMessage.repository.UserNoticeConfigRepository;
import com.ftf.tp.message.infrastructure.converter.NoticeConfigConverter;
import com.ftf.tp.message.infrastructure.mapper.NoticeSettingConfigMapper;
import com.ftf.tp.message.infrastructure.mapper.UserNoitceConfigMapper;
import com.ftf.tp.message.infrastructure.mapper.po.NoticeSettingConfig;
import com.ftf.tp.message.infrastructure.mapper.po.UserNoitceConfig;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author tanghao
 * @date 2022/9/16
 */
@Repository
public class UserNoticeConfigRepositoryImpl implements UserNoticeConfigRepository {

    @Resource
    private UserNoitceConfigMapper userNoitceConfigMapper;

    @Resource
    private NoticeSettingConfigMapper noticeSettingConfigMapper;

    /**
     * 用户通知配置是否存在
     *
     * @param userId     用户di
     * @param i          消息类型
     * @param targetType 消息主体对象类型
     * @return 结果
     */
    @Override
    public boolean exitConfig(Long userId, int i, String targetType) {
        NoticeSettingConfig noticeSettingConfig = noticeSettingConfigMapper.selectOne(
                new QueryWrapper<NoticeSettingConfig>().eq("type", i).eq("target_type", targetType));
        UserNoitceConfig userNoitceConfig = userNoitceConfigMapper.selectOne(
                new QueryWrapper<UserNoitceConfig>().eq("user_id", userId).eq("config_id", noticeSettingConfig.getId()));

        return ObjectUtils.isNotEmpty(userNoitceConfig);
    }

    /**
     * 获取通知配置
     *
     * @param i          消息类型
     * @param targetType 消息主体对象类型
     */
    @Override
    public NoticeSettingConfigDO getNoticeSettingConfig(int i, String targetType) {
        NoticeSettingConfig noticeSettingConfig = noticeSettingConfigMapper.selectOne(
                new QueryWrapper<NoticeSettingConfig>().eq("type", i).eq("target_type", targetType));
        return NoticeConfigConverter.INSTANCE.poToDo(noticeSettingConfig);
    }
}
